class Solution:
    def countAndSay(self, n: int) -> str:
        series = '1'
        if n == 1:
            return series
        return self.appearance(series, n-1)

    def appearance(self, series: str, n: int):
        i, cnt = 0, 1
        result = ''
        for i in range(1, len(series)):
            # 相邻的两个相等，计数加1
            if series[i - 1] == series[i]:
                cnt += 1
            else:
                # 相邻的不相等，把前几个相同数字计数结束并记录，并把计数初始为1
                result += str(cnt) + series[i - 1]
                cnt = 1
        if cnt > 0:
            result += str(cnt) + series[i]
        print('appearance:', result)
        n -= 1
        if n > 0:
            # 递归调用
            result = self.appearance(result, n)
        return result


solution = Solution()
res = solution.countAndSay(1)
print(res)
